package cn.itcast.nems.studentrecord.web.controller.b;

import cn.itcast.ic.common.bean.Page;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.journal.dto.JournalDropoutCreateDTO;
import cn.itcast.nems.journal.service.JournalDropoutService;
import cn.itcast.nems.manager.util.RequestToEntityConverter;
import cn.itcast.nems.order.dto.JournalDropoutDTO;
import cn.itcast.nems.studentrecord.dao.condition.ApprovalType;
import cn.itcast.nems.studentrecord.dao.condition.JournalDropoutParams;
import cn.itcast.nems.studentrecord.dao.dataobject.JournalDropoutDO;
import cn.itcast.nems.studentrecord.dto.SettlementResultDTO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Map;

import static cn.itcast.nems.journal.util.TransferUtils.formatPrice;

/**
 * <p>
 * 退学凭单-头 前端控制器
 * </p>
 *
 * @author 陈早庆
 * @since 2023-12-07
 */
@Controller
@RequestMapping("/b/journal/dropout")
public class JournalDropoutController {
    @Autowired
    private JournalDropoutService journalDropoutService;

    /**
     * 退学分页列表（我提交的）
     * 1.筛选：学员姓名、创建时间、状态
     * 2.【更多筛选】包括：凭单ID、学号、退学类型、退学原因、退学班级、退费单号、创建人、创建时间
     *
     * @return
     */
    @GetMapping("/{rangeType}")
    public ResponseEntity<Page<JournalDropoutDO>> list(HttpServletRequest request, @PathVariable("rangeType") String rangeType, @RequestParam(value = "page_number", required = false) Integer pageNumber, @RequestParam(value = "page_size", required = false) Integer pageSize) {
        JournalDropoutParams myEntity = (JournalDropoutParams) RequestToEntityConverter.convert(request, JournalDropoutParams.class);
        ApprovalType rangeTypeEnum = ApprovalType.valueOf(rangeType.toUpperCase());
        myEntity.setApprovalType(rangeTypeEnum);
        BasePageRequest<JournalDropoutParams> params = new BasePageRequest<>();
        params.setPageNumber(pageNumber);
        params.setPageSize(pageSize);
        params.setCondition(myEntity);
        params.convert();
        // 条件
        JournalDropoutParams condition = params.getCondition();
        return ResponseEntity.ok().body(journalDropoutService.listPage(pageNumber, pageSize, condition));
    }

    // 班級下拉
    @GetMapping(value = "/clazzes/{rangeType}")
    public ResponseEntity<Page<Map>> searchClazzesCommit(HttpServletRequest request, @PathVariable("rangeType") String rangeType, @RequestParam(value = "page_number", required = false) Integer pageNumber, @RequestParam(value = "page_size", required = false) Integer pageSize) {
        JournalDropoutParams myEntity = (JournalDropoutParams) RequestToEntityConverter.convert(request, JournalDropoutParams.class);
        ApprovalType rangeTypeEnum = ApprovalType.valueOf(rangeType.toUpperCase());
        myEntity.setApprovalType(rangeTypeEnum);
        BasePageRequest<JournalDropoutParams> params = new BasePageRequest<>();
        params.setPageNumber(pageNumber);
        params.setPageSize(pageSize);
        params.setCondition(myEntity);
        params.convert();
        // 条件
        JournalDropoutParams condition = params.getCondition();
        return ResponseEntity.ok(journalDropoutService.searchClazzes(pageNumber, pageSize, condition));
    }

    @GetMapping
    public ResponseEntity<JournalDropoutDO> getJournalDropoutById(@RequestParam("id") String id) {
        return ResponseEntity.ok().body(journalDropoutService.getJournalDropoutById(id));
    }

    @PostMapping
    public ResponseEntity<String> create(@RequestBody JournalDropoutCreateDTO params) {
        if (params.getInvoiceFlag() == null) {
            params.setInvoiceFlag(Boolean.FALSE);
        }
        if (params.getLoanFlag() == null) {
            params.setLoanFlag(Boolean.FALSE);
        }
        return ResponseEntity.ok().body(journalDropoutService.create(params));
    }

    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Boolean> delete(@PathVariable("id") String id) {
        return ResponseEntity.ok().body(journalDropoutService.delete(id));
    }

    @PutMapping
    public ResponseEntity<Boolean> update(@RequestBody JournalDropoutDTO params) {
        if (params.getInvoiceFlag() == null) {
            params.setInvoiceFlag(Boolean.FALSE);
        }
        if (params.getLoanFlag() == null) {
            params.setLoanFlag(Boolean.FALSE);
        }
        return ResponseEntity.ok().body(journalDropoutService.update(params));
    }

    /**
     * 提交
     *
     * @param id 升级凭单ID
     * @return 是否成功
     **/
    @PutMapping("/flow/{id}")
    public ResponseEntity<Boolean> flow(@PathVariable("id") String id) {
        return ResponseEntity.ok(journalDropoutService.flow(id));
    }

    /**
     * 退学结算获取以下金额：
     * 应交金额
     * 应服务天数
     * 实际服务天数
     * 课耗金额
     * 已交金额
     * 剩余金额
     *
     * @param studentRecordId 学籍id
     * @param settlementDate  出班结算日期
     * @return
     */
    @GetMapping(value = "/settlement")
    public ResponseEntity<SettlementResultDTO> settlementCalculatorRefund(@RequestParam(value = "student_record_id") String studentRecordId, @RequestParam(value = "settlement_date") LocalDate settlementDate, @RequestParam(value = "type") String type) {
        SettlementResultDTO result = journalDropoutService.settlementCalculatorRefund(studentRecordId, settlementDate, type);
        if (StringUtils.hasText(result.getSettlementAmount())) {
            result.setSettlementAmount(formatPrice(Integer.valueOf(result.getSettlementAmount())));
        }
        if (StringUtils.hasText(result.getSettlementArrearsAmount())) {
            result.setSettlementArrearsAmount(formatPrice(Integer.valueOf(result.getSettlementArrearsAmount())));
        }
        if (StringUtils.hasText(result.getPaidAmount())) {
            result.setPaidAmount(formatPrice(Integer.valueOf(result.getPaidAmount())));
        }
        if (StringUtils.hasText(result.getActualAmountDue())) {
            result.setActualAmountDue(formatPrice(Integer.valueOf(result.getActualAmountDue())));
        }
        if (StringUtils.hasText(result.getRefundAmount())) {
            result.setRefundAmount(formatPrice(Integer.valueOf(result.getRefundAmount())));
        }
        return ResponseEntity.ok().body(result);
    }
}
